﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Pixcom
{




    public static class PicProtocal //partial class Pixcom_Form : Form
    {

        public const int LinkHeartRate = 6*3;//基数为 10s 6 为1分钟 *5 5分鐘
        public const byte PACKAGE_UNKNOW = 0x00; //未知数据包

        public const byte PUPLOAD_OK = 0xA0; //上传完成状态

        public const byte RSCMD_ASK_PARA = 0x01;
        public const byte RSACK_ASK_PARA = 0x02;
        public const byte RSCMD_SET_PARA = 0x03;
        public const byte RSACK_SET_PARA = 0x04;
        public const byte RSCMD_SINGLE_SHOT = 0x05;
        public const byte RSACK_SINGLE_SHOT = 0x06;
        public const byte RSCMD_UPLOAD = 0x07;
        public const byte RSACK_UPLOAD = 0x08;

        public const byte RSCMD_FOCUS = 0x09;
        public const byte RSACK_FOCUS = 0x0A;

        public const byte RSCMD_SYSTEM = 0x10;
        public const byte RSACK_SYSTEM = 0x11;

        public const byte RSCMD_MULTI_SHOT = 0x20;
        public const byte RSACK_MULTI_SHOT = 0x21;
        public const byte RSCMD_ASK_MULTIDONE = 0x22;
        public const byte RSACK_ASK_MULTIDONE = 0x23;
        public const byte RSCMD_ASK_MULTILEN = 0x24;
        public const byte RSACK_ASK_MULTILEN = 0x25;

        public const int SP_TIMEUP_VALUE = 1;
        public const int SP_INBUFFER_MAX_SIZE = 2100;       //2100 byte
        public const int OVT_REPEAT_LIMIT = 3;
   
        public static int BUF_SIZE = 65536;

        public static string ONLINE_IMG = "online.png";
        public static string OFFLINE_IMG = "offline.png";
        public static string TIMSTR = "yyyy-MM-dd HH:mm:ss:ffff";

        public static bool ParsePackage(ref byte[] BUF, ref int BUFLen ,ref int addr, ref int PackageLen,ref byte[] Package)
        {
            //解析报文
            bool success = false;
            int saylen = 0;
            for (int i = 0; i < BUFLen - 12; i++)
            {
                //数据长度尾数为08&& (BUF[i+2] == 0x08) //本本长度为8   && (BUF[i + 2] == 0x08)
                if ( (BUF[i] == 0x7E) )       //检测最新收到的数据看看是否跟包尾标志一致 
                {
                    saylen = BUF[i + 1] * 256 + BUF[i + 2] + 5;     //获得包中声明长度 5 字节 包括  包头 1字节包尾 1字节 数据长度 2字节 校验和 1字节


                    if (BUFLen >= (i + saylen))
                    {


                        if ((BUF[(i + saylen - 1)] == 0xE7)) //声明长度+5跟已接收实际长度一致，才去处理接收包
                        {
                         
                            PackageLen = saylen;
                            Package = new byte[saylen];
                            //拷贝一个完整的数据包
                            Array.Copy(BUF, i, Package, 0, saylen);
                            if (CheckSum(Package))
                            {
                                 success = true;
                                 addr = BUF[3 + i];
                                 BUFLen -= i + saylen;

                                //移除已经拷贝的完整的数据包
                                 Array.Copy(BUF, i + saylen, BUF, 0, BUFLen);
                            }
                            else {
                                Logger.Error("校验和错误:\n{0} \n", PicProtocal.byteToHexStr(Package, 0, PackageLen));

                            }
                                

                           
                            


                        }
                    }
                }
            }

            return success;
        }
        /************************************************
        *       串口接收处理函数，按照接收到的不同应答作相应处理函数调用
        * **********************************************/
        public static String RS_RcvHandler(ImageObject imgobj)
        {
            String result = "";
        //    if (CheckSum(imgobj.Package))
         //   {
                imgobj.PackageType = imgobj.Package[4];
               // tmrCmdOVT.Stop(); //停止超时重发计时器
                switch (imgobj.PackageType)       //查看CMD字节
                {
                    //收到查询参数应答
                    case RSACK_ASK_PARA:
                        {
                            result= RSACK_ASK_PARA_Handler(imgobj);
                        }
                        break;

                    //收到设置参数应答
                    case RSACK_SET_PARA:
                        {
                            result= RSACK_SET_PARA_Handler(imgobj);
                        }
                        break;

                    //收到对焦操作应答
                    case RSACK_FOCUS:
                        {
                            result= RSACK_FOCUS_Handler(imgobj);
                        }
                        break;                        

                    //收到系统操作应答
                    case RSACK_SYSTEM:
                        {
                            result= RSACK_SYSTEM_Handler(imgobj);
                        }
                        break;

                    //收到拍摄完毕应答
                    case RSACK_SINGLE_SHOT:
                        {
                            result= RSACK_SINGLE_SHOT_Handler(imgobj);
                        }
                        break;                                           

                    //收到数据包，处理它
                    case RSACK_UPLOAD:
                        {
                            result= RSACK_UPLOAD_Handler(imgobj);
                        }
                        break;

                    //收到快速连拍应答，表示多拍进行中，更新超时计时器轮询连拍进度
                    case RSACK_MULTI_SHOT:
                        {
                            result= RSACK_ASK_MULTISHOT_Handler(imgobj);
                        }
                        break;

                    // 收到连拍进度答复
                    case RSACK_ASK_MULTIDONE:
                        {
                            result= RSACK_ASK_MULTIDONE_Handler(imgobj);
                        } 
                        break;

                    // 收到图片长度信息应答
                    case RSACK_ASK_MULTILEN:
                        {
                            result= RSACK_ASK_MULTILEN_Handler(imgobj);
                        }
                        break;

                    default:
                        {

                            result = String.Format("收到设备上报但指令未定义的包"); 
                        }
                        break;
                }
                 
         //   }
         //   else {
         //       result = String.Format("校验和错误");
         //   }
            imgobj.Package = new byte[1];
            imgobj.PackageLen = 0;
            return result;
        }


        /************************************************
        *       查询参数应答处理函数
        * **********************************************/
        public static String RSACK_ASK_PARA_Handler(ImageObject imgobj)
        {
            String result = "",comboSet="",pktsz="",eqpid="0";
            switch (imgobj.Package[5])   //波特率
            {
                case 1: comboSet = "9600";break;
                case 2: comboSet = "19200"; break;
                case 3: comboSet = "38400"; break;
                case 4: comboSet = "57600"; break;
                case 5: comboSet = "115200"; break;
                case 6: comboSet = "230400"; break;
                case 7: comboSet = "460800"; break;
                case 8: comboSet = "921600"; break;                
            }

            imgobj.eqpid = imgobj.Package[6];      //ID
         
            switch (imgobj.Package[7])   //包容量
            {
                case 1: pktsz = "256"; break;
                case 2: pktsz = "512"; break;
                case 3: pktsz = "1024"; break;
                case 4: pktsz = "2048"; break;
            }
          
            if (imgobj.Package[9] == 1) 
            {
                MessageBox.Show("当前模块为“种子机”，CPU只有IAP引导代码Bootloader，未烧录主程序固件。\n"+
                                "请先用配套“IAP烧录软件”下载主程序固件",                    
                    "固件异常", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            string intf = ((byte)(imgobj.Package[10] & 0x80)) == 0 ? "UART-" : "SPI-";
            byte hwver = (byte)((imgobj.Package[10] >> 5) & 0x03);
            byte swver = (byte)(imgobj.Package[10] & 0x1F);            
         
             

            result = String.Format("当前设备信息,Ver:{0},{1},设备地址{1}，波特率{2}，数据包大小{3}",
                                 intf + hwver.ToString() + "." + swver.ToString(),
                                 eqpid, comboSet,pktsz);
            return result;
        }


        /************************************************
        *       设置参数应答处理函数
        * **********************************************/
        public static String RSACK_SET_PARA_Handler(ImageObject imgobj)
        {
            String result = "";
            if(imgobj.Package[5] == 0x00)
            {
                result = "参数设置成功";
            //  lblState.Text
            }
            else
            {
                result = "参数设置失败";                
            }
            return result;
        }


        /************************************************
        *       对焦操作应答处理函数
        * **********************************************/
        public static String RSACK_FOCUS_Handler(ImageObject imgobj)
        {
            String result = "";
            switch (imgobj.Package[5])
            {
                case 0:
                    imgobj.stImg.VCM = imgobj.Package[6] * 256 + imgobj.Package[7];
                    //lblFocusState.Text 
                        result = "指令完成，当前焦距:" + imgobj.stImg.VCM; break;
                case 1:
                    result = "参数出错，设置失败";
                    break;
                case 2:
                    result = "传感器故障！！";
                    break;
            }
            return result;
        }


        


        /************************************************
        *       系统操作应答处理函数
        * **********************************************/
        public static String RSACK_SYSTEM_Handler(ImageObject imgobj)
        {
            String result = "";
            if (imgobj.Package[5] == 0)
            {
                switch (imgobj.Package[6])
                {
                    case 1: result = "已恢复出厂设置"; break;
                    case 2: result = "已执行复位指令"; break;                    
                }
            }
            return result;
        }


        /************************************************
       *       单拍应答处理函数
       * **********************************************/
        public static String RSACK_SINGLE_SHOT_Handler(ImageObject imgobj)
        {
            String result = "";
         
            if (imgobj.Package[5] == 0)      //拍摄成功
            {   //7E 08 08 00 08 01      00 18 00              19 00 
                //7E 00 08 00 06 00      00 C2 D0              00 19   B9 E7 

                imgobj.stImg.ImgLenArr[0] = imgobj.Package[6] * 65536 + imgobj.Package[7] * 256 + imgobj.Package[8];//图像的字节数
                imgobj.stImg.ImgPktArr[0] = imgobj.Package[9] * 256 + imgobj.Package[10];//图像的总包数
                imgobj.stImg.Pktsz = imgobj.stImg.ImgPktArr[0];
                imgobj.stImg.ImgTotLen = imgobj.stImg.ImgLenArr[0];
                //发出读数指令
                imgobj.stImg.PktInx = 1;
                imgobj.stImg.ImgInx = 0;
                imgobj.stImg.ImgLen = 0;
                imgobj.stImg.ImgTotNum = 1;
                int i = imgobj.stImg.PktInx, j = imgobj.stImg.ImgPktArr[0];

                byte [] send=   RS_PacketTx(imgobj.Package[3], RSCMD_UPLOAD, 5, (byte)(imgobj.stImg.ImgInx + 1), (byte)(i >> 8), (byte)i, (byte)(j >> 8), (byte)j);
                //设置到参数中
                imgobj.bufsend = send;
                imgobj.bytesSend = send.Length;

                string tsm = System.DateTime.Now.ToString(TIMSTR);
                tsm = tsm.Replace("/", "");
                tsm = tsm.Replace(":", "");
                tsm = tsm.Replace(" ", "-");

                imgobj.filename =  "IMG-" + tsm+".jpg";


                result = "拍摄成功,开始读图";
            }
            else
            {
                switch (imgobj.Package[5])
                {
                    case 1: result = "参数出错，重新设置"; break;
                    case 2: result = "传感器故障，断电排错"; break;
                    case 3: result = "拍摄正忙，稍后再按"; break;                        
                }                    
            }
            return result;
        }


        /************************************************
        *       上传数据应答处理函数
        *       allP = 0;//数据包总长度
                curP = 0;//当前包序号
        * **********************************************/
        public static String RSACK_UPLOAD_Handler(ImageObject imgobj)
        {
            String result;
            //Image img_tmp;
            int datalen = 0;
       
    
                datalen = imgobj.Package[1] * 256 + imgobj.Package[2] - 8;    //本包有效数据长度
                 int pktIndex= imgobj.Package[6] * 256 + imgobj.Package[7];//当前包序号;
                int pktsz= imgobj.Package[8] * 256 + imgobj.Package[9];//数据包总长度
                if (pktsz == 0) {
                  Logger.Error("数据格式出错！重新请求>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");

                }
                imgobj.stImg.PktInx = pktIndex;
                imgobj.stImg.Pktsz = pktsz;
               SaveImageToDisk(imgobj);
               // Array.Copy(imgobj.Package, 11, imgobj.stImg.ImgBuf, imgobj.stImg.ImgLen, datalen);      //把有效字节拷贝进图像主缓存

                imgobj.stImg.ImgLen += datalen;
               
                
                result = "共" + imgobj.stImg.Pktsz + "包，" + "正读取第" + imgobj.stImg.PktInx + "包";      
                      
              //  LoadingBar.Value = curP / allP;

                //接收的字节数已经足够，一帧图片已经保存
                if (imgobj.stImg.PktInx >= imgobj.stImg.Pktsz)
                {

                result += "图像接收完毕！";
                imgobj.stImg.PktInx = 0;
                imgobj.stImg.Pktsz = 0;
                imgobj.PackageType = PicProtocal.PUPLOAD_OK;
                imgobj.ImageOK = true;
                 //  img_tmp = CreatImg(imgobj.stImg.ImgBuf, imgobj.stImg.ImgLen);     //在主画面显示图像

                //  if (img_tmp != null)
                //  {
                //    img_tmp.Save(imgobj.path);
                //   //  //自动保存
                //  if (chkAutoSave.Checked == true)
                //  {
                //      string tsm = System.DateTime.Now.ToString();
                //      tsm = tsm.Replace("/", "");
                //      tsm = tsm.Replace(":", "");
                //      tsm = tsm.Replace(" ", "-");

                //      string FN = AutosaveFolderDlg.SelectedPath + "\\IMG-" + tsm;

                //      if (rbMultiShot.Checked == true)        //快连拍自动保存在文件名最后加（N）
                //      {
                //          FN = FN + "(" + (stImg.ImgInx + 1).ToString() + ")";
                //      }
                //      FN = FN + ".jpg";
                //      pbMain.Image.Save(FN);
                //  }

                //  stImg.ImagineArr[stImg.ImgInx++] = img_tmp;  //把成功获取的图片放入数组
                ////  lblShowImgNum.Text = "已接收" + stImg.ImgInx.ToString() + "，" + "显示第" + stImg.ImgInx.ToString();
                //  if (stImg.ImgInx < stImg.ImgTotNum)  //还有图像数据未读完，继续新一轮读图
                //  {
                //      stImg.ImgLen = 0;
                //      stImg.PktInx = 1;
                //      int i = stImg.PktInx, j = stImg.ImgPktArr[stImg.ImgInx];
                //      RS_PacketTx(CalculatePKTms(), RSCMD_UPLOAD, 5, (byte)(stImg.ImgInx + 1), (byte)(i >> 8), (byte)i, (byte)(j >> 8), (byte)j);                            
                //  }
                //  else
                //  {
                //      //连拍
                //      if (rbConstShot.Checked == true)
                //      {
                //          stImg.ImgLen = 0;
                //          stImg.PktInx = 0;
                //          stImg.ImgInx = 0;
                //          stImg.ImgTotNum = 0;                                
                //          RS_PacketTx(1000, RSCMD_SINGLE_SHOT, 6, stSP.ConstShotCMD);                                
                //          lblState.Text = "发出拍摄指令";
                //      }
                //      else   //已经接收完毕，允许翻页看图
                //      {                                
                //          gpBoxShow.Enabled = true;
                //          btnStart.Enabled = true;
                //          GP_ShotSetting.Enabled = true;
                //          stImg.ImgShowCnt = 0;
                //          lblState.Text = "当前图像 " + stImg.ImgLenArr[stImg.ImgShowCnt].ToString() + " B，全部共 " + stImg.ImgTotLen.ToString() + " B";
                //          lblShowImgNum.Text = "已接收" + stImg.ImgInx.ToString() + "，" + "显示第" + (stImg.ImgShowCnt + 1).ToString();
                //          pbMain.Image = stImg.ImagineArr[stImg.ImgShowCnt];
                //      }
                //  }
                //    }
                //  else   //图片解码出错，结束读取流程
                //   {

                //    }


            }
                else   //还有数据未读出，发出读数指令 
                {
                int i = imgobj.stImg.PktInx + 1, j = imgobj.stImg.Pktsz;// imgobj.stImg.ImgPktArr[imgobj.stImg.ImgInx];

                byte[] send=    RS_PacketTx(imgobj.Package[3], RSCMD_UPLOAD, 5,0x01, 
                      (byte)(i >> 8), (byte)i, (byte)(j >> 8), (byte)j);
                imgobj.bufsend = send;
                imgobj.bytesSend = send.Length;
            }
           
            return result;
        }



        /************************************************
        *       应答连拍指令
        * **********************************************/
        public static String RSACK_ASK_MULTISHOT_Handler(ImageObject imgobj)
        {
            String result = "";
            if (imgobj.Package[5] == 0x00)       //收到连拍指令并执行中
            {
                result = "已接收连拍指令，轮询进度中";
                RS_PacketTx(150, RSCMD_ASK_MULTIDONE, 0);
                imgobj.stOVT.TotReSend++;
            }
            else
            {
                switch (imgobj.Package[5])
                {
                    case 1: result = "参数出错，重新设置"; break;
                    case 2: result = "传感器故障，断电排错"; break;
                    case 3: result = "拍摄正忙，稍后再按"; break;
                }
            }
            return result;
        }



        /************************************************
          *       应答连拍进度
          * **********************************************/
        public static String RSACK_ASK_MULTIDONE_Handler(ImageObject imgobj)
        {
            String result = "";
           if (imgobj.Package[5] == 0x00)     //拍摄完毕，查询长度
            {
                result = "连拍完毕，查询图片长度";
                RS_PacketTx(150, RSCMD_ASK_MULTILEN, 0);
                if (imgobj.stOVT.TotReSend > 0)
                {
                    imgobj.stOVT.TotReSend--;
                }
             //   lblOVTRepe.Text = imgobj.stOVT.TotReSend.ToString();
            }
           else      //连拍未完成，要延时后继续查进度
            {
                if (imgobj.stOVT.TotReSend > 0)
                {
                    imgobj.stOVT.TotReSend--;
                }
                result = "连拍未完成，查询图片长度";
              //  ResetOVT_and_Start(500);
            }
            return result;
        }


        /************************************************
        *       查询多拍长度应答处理函数
        * **********************************************/
        public static String RSACK_ASK_MULTILEN_Handler(ImageObject imgobj)
        {
            int i, j;
            String result;
            imgobj.stImg.ImgTotNum = 0;
            imgobj.stImg.ImgTotLen = 0;
            for (i = 0; i != 20; i++)
            {
                imgobj.stImg.ImgLenArr[i] = imgobj.Package[5 + i * 5] * 65536 + imgobj.Package[6 + i * 5] * 256 + imgobj.Package[7 + i * 5];
                imgobj.stImg.ImgPktArr[i] = imgobj.Package[8 + i * 5] * 256 + imgobj.Package[9 + i * 5];
                if (imgobj.stImg.ImgLenArr[i] > 0)
                {
                    imgobj.stImg.ImgTotNum++;
                    imgobj.stImg.ImgTotLen += imgobj.stImg.ImgLenArr[i];
                }
            }
            //发出读数指令
            imgobj.stImg.ImgInx = 0;
            imgobj.stImg.ImgLen = 0;
            imgobj.stImg.PktInx = 1;
            i = imgobj.stImg.PktInx;
            j = imgobj.stImg.ImgPktArr[imgobj.stImg.ImgInx];
            byte[] send= RS_PacketTx(imgobj.Package[3],RSCMD_UPLOAD, 5, (byte)(imgobj.stImg.ImgInx + 1), (byte)(i >> 8), (byte)i, (byte)(j >> 8), (byte)j);
            imgobj.bufsend = send;
            imgobj.bytesSend = send.Length;
            result = "拍摄完成，开始读图";
            return result;
        }





        /************************************************
      *       串口接收应答包检验函数
      * **********************************************/
        public static bool CheckSum(byte[] buf)      //检验接收包的和，len表示包中要检验的数据长度
        {
            int sum = 0, chk = 0, i = 1, len;
            len = buf[1] * 256 + buf[2] +2;

            if(len>= SP_INBUFFER_MAX_SIZE)
            {
                return false;
            }
            else
            {
                for (; i < 1+len; i++)
                {
                    sum += buf[i];
                }
                chk = buf[i];
                sum = sum % 256;
                return (sum == chk);
            }
        }
        public static byte[]  single_short(ImageObject imgobj) {
 

            byte[] para = new byte[6];

            imgobj.stSP.ForceStop = false;
            para[0] = imgobj.imgParam.resolution;            //选择图像分辨率
            para[1] = imgobj.imgParam.quanlity;//图像质量
            para[1]|= imgobj.imgParam.colorable;    //选择图像色彩 

            para[2] = imgobj.imgParam.format;    //选择图像色彩 
            para[3] = imgobj.imgParam.exposure;
            if (imgobj.imgParam.exposure==(byte)EXPOSURE.MANUAL) {
                para[3] |= imgobj.imgParam.exposureVal;
            }
            para[4] = para[5] = 0;
            imgobj.stImg.Pktsz = 1;//设置为1 成功之后会设置为 实际的只，只是在判断状态使用该值

            byte[] send = RS_PacketTx(imgobj.eqpid,PicProtocal.RSCMD_SINGLE_SHOT,6,para);
            imgobj.bufsend = send;
            imgobj.bytesSend = send.Length;
            return send;

        }

        /************************************************
      *       串口打包发送函数
      *       @ cmd 发送的命令 
      *       @ num 发送的参数的长度
      *       @ para 发送的参数字节
      *       return 需要发送的字节
      * **********************************************/
        public static byte[] RS_PacketTx(byte  addr, byte cmd, int num, params byte[] para)
        {
            byte[] SndBuf = new byte[13];
             SndBuf[0] = 0x7E;     //协议头
             SndBuf[1] = 0x00;
             SndBuf[2] = 0x08;
  
             SndBuf[3] =  addr ;
             SndBuf[4] = cmd;
            int i = 0;
            for (; i != num; i++)   //写入参数
            {
                 SndBuf[5 + i] = para[i];                
            }
            for (; i != 6; i++)     //补0
            {
                SndBuf[5 + i] = 0;
            }
            int sum = 0;
            for (i = 1; i != 11; i++)
            {
                sum += SndBuf[i];
            }
             SndBuf[11] = (byte)(sum % 256);
             SndBuf[12] = 0xE7;
            return SndBuf;
 
        }
        public static string byteToHexStr(byte[] bytes, int offset,int len)
        {
            // PicProtocal.byteToHexStr() string stx = PicProtocal.byteToHexStr(RS_RcvBuf, 0, stSP.RcvCnt);
            //string returnStr = "";
            //if (bytes != null)
            //{
            //    for (int i = 0; (i < bytes.Length) && (i < len); i++)
            //    {
            //        returnStr += bytes[i].ToString("X2") + " ";
            //    }
            //}
            return BitConverter.ToString(bytes, offset, len).Replace("-"," ");
        }

        public static void SaveImageToDisk(ImageObject imgobj) {
            int datalen = 0;

            //存在写文件出错的情况，会偶尔丢失 一个数据包，应该在写入判断是否有缓存数据，有操作缓存数据，再操作当前数据
            try
            {
                datalen = imgobj.Package[1] * 256 + imgobj.Package[2] - 8;    //本包有效数据长度
                if (!File.Exists(imgobj.dir))
                {
                    if (!Directory.Exists(imgobj.dir))
                        Directory.CreateDirectory(imgobj.dir);
                    //File.Create(ProjectFullName);
                }
                imgobj.path = imgobj.dir + imgobj.filename;
                FileStream fs = new FileStream(imgobj.path, FileMode.Append | FileMode.OpenOrCreate);//以Append方式打开文件
                BinaryWriter bw = new BinaryWriter(fs);  //二进制写入流
                try
                {
                    if (imgobj.stImg.ImgLen > 0)
                    {
                        bw.Write(imgobj.stImg.ImgBuf, 0, imgobj.stImg.ImgLen);
                        imgobj.stImg.ImgLen = 0;
                    }

                }
                catch (Exception ) {
                    Logger.Info("写入文件-缓存{0}", imgobj.path);
                }
               

                bw.Write(imgobj.Package, 11, datalen);  //写文件

                bw.Close();
                fs.Close();
                Logger.Info("写入文件{0}", imgobj.path);
            }
            catch (Exception e)
            {
                Array.Copy(imgobj.Package, 11, imgobj.stImg.ImgBuf, imgobj.stImg.ImgLen, datalen);      //把有效字节拷贝进图像主缓存
                Logger.Error("寫入文件出錯{0}",e.ToString());
            }
 
         }


      //  /************************************************
      //*       图片生成函数
      //* **********************************************/
      //  public static Image CreatImg(byte[] src, int len)
      //  {
      //      try
      //      {
      //          MemoryStream img_ms = new MemoryStream(src, 0, len);
      //          Image img = System.Drawing.Image.FromStream(img_ms);
      //          if ((pbMain.Width <= img.Width) || (pbMain.Height <= img.Height))      //只要画框长宽尺寸任意一个小于图像尺寸，就用ZOOM，否则用Center
      //          {
      //              pbMain.SizeMode = PictureBoxSizeMode.Zoom;
      //          }
      //          else
      //          {
      //              pbMain.SizeMode = PictureBoxSizeMode.CenterImage;
      //          }
      //          pbMain.Image = img;
      //          return img;
      //      }
      //      catch (Exception ex)
      //      {
      //          MessageBox.Show(ex.Message + "\r\n图像数据解码出错，已停止接收", "出错了", MessageBoxButtons.OK, MessageBoxIcon.Error);
      //          return null;
      //      }
      //  }

       

    }
}